import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-device-repaired',
  templateUrl: './device-repaired.component.html'
})
export class DeviceRepairedComponent implements OnInit {
  tableData = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  searchKey: string = '' //搜索keyword
  repairContent: string = '' //查询维修内容参数
  faultPhenomenon: string = '' //查询故障参数
  selectedCompanyName: string = '' //查询公司参数
  selectedCompanyId: string = '' //查询公司参数
  deviceId: string = '' //查询设备参数
  carNumber: string = '' //查询车牌号码参数
  startTime = '' //查询开始时间参数
  endTime = '' //查询结束时间参数
  dateType: string = '1' //时间类型
  sortType: string = '2'
  pTypeList: any = ['离线问题', 'GPS问题', '传感器问题', '甩挂问题']
  typeList: any = [
    '更换主机',
    '电源维修',
    '更换线材',
    '挪动天线位置',
    '更换天线',
    'V4换V7',
    '更换传感器',
    '重新焊接',
    '离线其他',
    'GPS其他',
    '传感器其他',
    '无维修'
  ]
  carTypeList = carTypeListObj.carTypeList
  carTypeText = carTypeListObj.carDeatailUseList
  inputCompanyTreeCarData: any = {} //车辆运行状态模块跳转该页面传递过来的参数
  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage == undefined ? 1 : queryParams.nowPage
      this.inputCompanyTreeCarData = JSON.parse(common.getCookie('jumpRepairedDeviceList'))
    })
  }

  ngOnInit() {
    this.initBtn()
    this.initDate()
    this.load()
  }
  btnData: any = []
  resourceExport: boolean = false //导出按钮权限
  factoryLimit: boolean = false //查看进出厂限制
  //初始化权限按钮
  initBtn() {
    this.btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_device:repaired:customer')))
    console.log('btnData', this.btnData)
    if (!this.btnData) {
      return
    }
    for (let item of this.btnData) {
      if (item.sourceKey === 'deviceRepaired:export' && item.parentId) {
        this.resourceExport = true
      }
    }
  }
  ngOnDestroy() {
    document.onclick = null
  }
  // 数据导出
  exportData() {
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        dateType: this.dateType,
        sortType: this.sortType,
        searchKey: this.searchKey,
        companyId: this.selectedCompanyId,
        deviceId: this.deviceId,
        useTypeL2: $('#TcarTypeSelect2').val(),
        repairContent: this.repairContent,
        faultPhenomenon: this.faultPhenomenon,
        startTime: this.startTime,
        endTime: this.endTime,
        isByAuth: true,
        isExport: true
      }
    }

    common.exprotDataFile(data, 'gridPager', config.BaseURL + 'deviceRepaired/queryDeviceRepairedRecords.html')
  }
  //初始化时间
  initDate() {
    var that = this
    return new Promise(function (resolve, reject) {
      System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(() => {
        System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(() => {
          $('#datetimepicker1')
            .datetimepicker({
              format: 'yyyy-mm-dd hh:ii:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              $('#datetimepicker2').datetimepicker('setStartDate', time)
            })
          $('#datetimepicker2')
            .datetimepicker({
              format: 'yyyy-mm-dd hh:ii:ss',
              language: 'zh-CN',
              autoclose: true
            })
            .on('changeDate', function (e) {
              var date = e.date
              var time = common.getTimeMmss(date)
              $('#datetimepicker1').datetimepicker('setEndDate', time)
            })
          resolve()
        })
      })
    })
  }
  //删除所选时间
  del_date() {
    this.startTime = ''
    this.endTime = ''
    $('#datetimepicker1').val('').change()
    $('#datetimepicker2').val('').change()
    $('#datetimepicker2').datetimepicker('setStartDate', null)
    $('#datetimepicker1').datetimepicker('setEndDate', null)
  }
  //table刷新
  refresh() {
    this.load()
  }
  //表格搜索
  table_search() {
    this.curPage = 1
    this.searchKey = common.stringTrim(this.searchKey)
    this.load()
  }
  //初始化加载list数据
  load() {
    this.startTime = $('#datetimepicker1').val()
    this.endTime = $('#datetimepicker2').val()
    let data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        dateType: this.dateType,
        sortType: this.sortType,
        searchKey: this.searchKey,
        companyId: this.selectedCompanyId,
        deviceId: this.deviceId,
        useTypeL2: $('#TcarTypeSelect2').val(),
        repairContent: this.repairContent,
        faultPhenomenon: this.faultPhenomenon,
        startTime: this.startTime,
        endTime: this.endTime,
        isByAuth: true,
        isExport: false
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(data)

    this.jsonApiService.http('deviceRepaired/queryDeviceRepairedRecords.html', reqdata).subscribe(result => {
      this.tableData = result.exhibitDatas || []
      this.tableData.map(item => (item.callDuration = common.secondToDate(item.callDuration)))
      this.totalCount = result.recordCount
    })
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }

  //公司选中回调
  companySelected(data) {
    this.selectedCompanyId = data.companyId
    this.selectedCompanyName = data.companyName
    this.deviceId = data.deviceId
    this.carNumber = data.carNumber
  }
}
